/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import ejb.Product;
import ejb.ProductFacadeLocal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author longdm0708l
 */
public class ProductBean {
    public void createProduct(Product product)
    {
        lookupProductFacade().create(product);
    }

    public void editProduct(Product product)
    {
        lookupProductFacade().edit(product);
    }

    public void removeProduct(Product product)
    {
        lookupProductFacade().remove(product);
    }

    private String ProductId;

    public String getProductId() {
        return ProductId;
    }

    public void setProductId(String ProductId) {
        this.ProductId = ProductId;
    }

    public List<Product> getAllProduct()
    {
        return lookupProductFacade().findAll();
    }

    public Product getProductById()
    {
        return lookupProductFacade().find(Integer.valueOf(getProductId()));
    }

    public List<Product> searchByName(String keyword)
    {
        return lookupProductFacade().searchByName(keyword);
    }

    private ProductFacadeLocal lookupProductFacade() {
        try {
            Context c = new InitialContext();
            return (ProductFacadeLocal) c.lookup("java:comp/env/ProductFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
